package JDBC3;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;

public class DruidDemo {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/JDBC3/DruidJdbc.properties");
        properties.load(fileInputStream);
        //使用德鲁伊数据库连接池技术
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        Connection connection = dataSource.getConnection();
        System.out.println("connection = " + connection);

        String sql = "select * from dept";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        ArrayList<Brand> brands = new ArrayList<>();

        while (resultSet.next()){
            Brand brand = new Brand();
            brand.setDEPTNO(resultSet.getInt("DEPTNO"));
            brand.setDNAME(resultSet.getString("DNAME"));
            brand.setLOC(resultSet.getString("LOC"));
            brands.add(brand);
        }
        System.out.println("brands = " + brands);
    }
}
